package com.jiazhong.boot.ioc.mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiazhong.boot.commons.bean.Emp;
import com.jiazhong.boot.commons.util.JsonResult;
import com.jiazhong.boot.commons.util.ResultTool;
import com.jiazhong.boot.ioc.mybatis.mapper.EmpMapper;
import com.jiazhong.boot.ioc.mybatis.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpMapper mapper;

    @Override
    public JsonResult findAll() {
        return ResultTool.success(mapper.findAll());
    }

    @Override
    public JsonResult findByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Emp> emps = mapper.findAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult find() {
        return ResultTool.success(mapper.find());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult update1(Emp emp, int id) {
        int row1 = mapper.removeById(id);
        System.out.println("删除执行：" + (row1 == 1 ? "成功" : "失败"));
        int row2 = mapper.save(emp);
        System.out.println("添加执行：" + (row2 == 1 ? "成功" : "失败"));
        return ResultTool.success("success");
    }
}
